package com.xfcy.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xfcy.blog.common.R;
import com.xfcy.blog.common.constant.Constant;
import com.xfcy.blog.common.constant.SqlConstant;
import com.xfcy.blog.mapper.MenuMapper;
import com.xfcy.blog.entity.Menu;
import com.xfcy.blog.service.MenuService;
import com.xfcy.blog.utils.BeanCopyUtil;
import com.xfcy.blog.vo.MenuVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 系统管理-权限资源表 (Menu)表服务实现类
 *
 * @author 晓风残月Lx
 * @since 2023-04-09 11:03:05
 */
@Service("menuService")
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    /**
     * 获取菜单树
     * @param menus
     * @return
     */
    @Override
    public List<MenuVO> listMenuTree(List<MenuVO> menus) {
        List<MenuVO> resultList = new ArrayList<>();
        for (MenuVO menu : menus) {
            Integer parentId = menu.getParentId();
            if ( parentId == null || parentId == 0)
                resultList.add(menu);
        }
        for (MenuVO menu : resultList) {
            menu.setChildren(getChildren(menu.getId(),menus));
        }
        return resultList;
    }



    /**
     * 菜单树列表
     * @return
     */
    @Override
    public R getMenuTree() {
        List<Menu> list = this.list();
        List<MenuVO> menuVOS = BeanCopyUtil.listCopy(list, MenuVO.class);
        List<MenuVO> result = listMenuTree(menuVOS);
        return R.success("获取菜单树成功", result);
    }

    /**
     * 后台获取菜单列表
     * @return
     */
    @Override
    public R getMenuApi(Integer id) {
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<Menu>()
                .eq("level", Constant.ONE).eq(id != null, SqlConstant.ID, id);
        List<Menu> menus = baseMapper.selectList(queryWrapper);
        for (Menu menu : menus) {
            List<Menu> childrens = baseMapper.selectList(new QueryWrapper<Menu>().eq(SqlConstant.PARENT_ID, menu.getId()));
            menu.setChildren(childrens);
        }
        return R.success(menus);
    }

    /**
     * 添加菜单
     * @param menu
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R insertMenu(Menu menu) {
        int insert = baseMapper.insert(menu);
        return insert > 0 ? R.success() : R.error("添加菜单失败！");
    }

    /**
     * 更新菜单
     * @param menu
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R updateMenu(Menu menu) {
        int update = baseMapper.updateById(menu);
        return update > 0 ? R.success() : R.error("更新菜单失败");
    }

    /**
     * 删除菜单
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R deleteMenuById(Integer id) {
        int rows = baseMapper.deleteById(id);
        return rows > 0 ? R.success(): R.error("删除菜单失败");
    }


    // ============自定义方法==================

    private List<MenuVO> getChildren(Integer pid, List<MenuVO> menus) {
        List<MenuVO> childrens = new ArrayList<>();
        for (MenuVO e: menus) {
            Integer parentId = e.getParentId();
            if(parentId != null && parentId.equals(pid)){
                // 子菜单的下级菜单
                childrens.add( e );
            }
        }
        // 把子菜单的子菜单再循环一遍
        for (MenuVO e: childrens) {
            // childrens
            e.setChildren(getChildren(e.getId(),menus));
        }
        //停下来的条件，如果 没有子元素了，则停下来
        if( childrens.size()==0 ){
            return null;
        }
        return childrens;
    }
}


